<!--
 * @Description: 
 * @Author: xiongkuobiao
 * @Date: 2023-02-28 10:41:55
 * @LastEditors: xiongkuobiao
 * @LastEditTime: 2023-02-28 11:05:42
-->
<!DOCTYPE html>
<html lang="en">
  <head>
    <meta charset="UTF-8" />
    <meta http-equiv="X-UA-Compatible" content="IE=edge" />
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <title>Document</title>
  </head>
  <body>
    <button class="btn">btn</button>
  </body>
  <script>
    let dom = document.querySelector(".btn");
    dom.onclick = debounce(
      function () {
        console.log(111);
      },
      1000,
      true
    );

    function debounce(fn, timer, target) {
      let timeout = null;
      return function () {
        if (timeout) clearTimeout(timeout);
        if (target) {
          fn();
          debugger;
          timeout = setTimeout(() => {
            timeout = null;
          }, timer);
        } else {
          timeout = setTimeout(() => {
            fn();
            timeout = null;
          }, timer);
        }
      };
    }
  </script>
</html>
